/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package encrypt;

/**
 *
 * @author XuanHung
 */
public class DESede extends DES
{
        private int[] key1 = null;
        private int[] key2 = null;
        private int[] key3 = null;

        private boolean encrypt;

        /**
         * standard constructor.
         */
        public DESede()
        {
        }

        /**
         * initialise a DES cipher.
         *
         * @param encrypting
         *            whether or not we are for encryption.
         * @param key
         *            the parameters required to set up the cipher.
         * @exception IllegalArgumentException
         *                if the params argument is inappropriate.
         */
        public void init(boolean encrypting, byte[] key)
        {
                key1 = generateWorkingKey(encrypting, key, 0);
                key2 = generateWorkingKey(!encrypting, key, 8);
                key3 = generateWorkingKey(encrypting, key, 16);

                encrypt = encrypting;
        }

        public String getAlgorithmName()
        {
                return "DESede";
        }

        public int getBlockSize()
        {
                return 8;
        }

        public void transformBlock(byte[] in, int inOff, byte[] out, int outOff)
        {
                if (key1 == null)
                {
                        throw new IllegalStateException("DESede engine not initialised!");
                }

                if (encrypt)
                {
                        desFunc(key1, in, inOff, out, outOff);
                        desFunc(key2, out, outOff, out, outOff);
                        desFunc(key3, out, outOff, out, outOff);
                }
                else
                {
                        desFunc(key3, in, inOff, out, outOff);
                        desFunc(key2, out, outOff, out, outOff);
                        desFunc(key1, out, outOff, out, outOff);
                }
        }

        public void reset()
        {
        }
}

